﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/assets/libs/layui/css/layui.css" />
    <link rel="stylesheet" href="/assets/module/admin.css?v=318">
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>
<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 表格工具栏 -->
                <form class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">角色名:</label>
                            <div class="layui-input-inline">
                                <input name="roleName" class="layui-input" placeholder="输入角色名" />
                            </div>
                        </div>
                        <div class="layui-inline">
                            &emsp;
                            <button class="layui-btn icon-btn" lay-filter="roleTbSearch" lay-submit perm-show="system:role:search">
                                <i class="layui-icon">&#xe615;</i>搜索
                            </button>
                        </div>
                    </div>
                </form>
                <!-- 数据表格 -->
                <table id="roleTable" lay-filter="roleTable"></table>
            </div>
        </div>
    </div>

    <!-- 表格操作列 -->
    <script type="text/html" id="roleTbBar">
        <a perm-show="system:role:edit" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
        <a perm-show="system:role:auths" class="layui-btn layui-btn-warm layui-btn-xs" lay-event="auth">权限分配</a>
    </script>
    <script type="text/html" id="toolbar">
        <a perm-show="system:role:edit" class="layui-btn layui-btn-sm icon-btn" lay-event="add"><i class="layui-icon">&#xe654;</i>新增</a>
        <a perm-show="system:role:add" class="layui-btn layui-btn-danger layui-btn-sm icon-btn" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</a>
    </script>

    <!-- 表单弹窗 -->
    <script type="text/html" id="roleEditDialog">
        <form id="roleEditForm" lay-filter="roleEditForm" class="layui-form model-form">
            <input name="roleId" type="hidden" />
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">角色名:</label>
                <div class="layui-input-block">
                    <input name="RoleName" placeholder="请输入角色名" class="layui-input"
                           lay-verType="tips" lay-verify="required" required />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">排序号:</label>
                <div class="layui-input-block">
                    <input name="RoleSort" placeholder="请输入排序号" class="layui-input" type="number"
                           lay-verType="tips" lay-verify="required|number" required />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">状态:</label>
                <div class="layui-input-block">
                    <input type="radio" name="RoleStatus" value="1" title="启用" checked>
                    <input type="radio" name="RoleStatus" value="0" title="禁用">
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn" lay-filter="roleEditSubmit" lay-submit>保存</button>
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            </div>
        </form>
    </script>

    <!-- js部分 -->
    <script src="/assets/libs/jquery/jquery-3.2.1.min.js"></script>
    <script src="/assets/libs/layui/layui.js"></script>
    <script src="/assets/js/utils.js"></script>
    <script src="/assets/js/main.js"></script>
    <script>
        layui.use(['layer', 'form', 'table', 'util', 'admin', 'setter', 'zTree', 'tableX'], function () {
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            var util = layui.util;
            var admin = layui.admin;
            var setter = layui.setter;

            // 初始设定
            layuiTableSet();

            /* 渲染表格 */
            var insTb = table.render({
                elem: '#roleTable',
                url: setter.baseServer + '/SystemManage/Role/GetPageListJson',
                page: true,
                toolbar: '#toolbar',
                cellMinWidth: 100,
                cols: [[
                    { type: 'checkbox' },
                    { type: 'numbers' },
                    { field: 'RoleName', title: '角色名', sort: true },
                    {
                        field: 'BaseCreateTime', title: '创建时间', templet: function (d) {
                            return util.toDateString(d.BaseCreateTime);
                        }, sort: true
                    },
                    { field: 'RoleSort', title: '排序号', align: 'center', width: 80 },
                    {
                        title: '状态', templet: function (d) {
                            if (d.RoleStatus == 1) {
                                return '<span class="layui-badge layui-badge-blue">启用</span>';
                            } else {
                                return '<span class="layui-badge layui-badge-red">禁用</span>';
                            }
                        }, align: 'center', width: 80
                    },
                    { title: '操作', toolbar: '#roleTbBar', align: 'center', width: 200, minWidth: 200 }
                ]]
            });

            /* 表格搜索 */
            form.on('submit(roleTbSearch)', function (data) {
                insTb.reload({ where: data.field, page: { curr: 1 } });
                return false;
            });

            /* 表格工具条点击事件 */
            table.on('tool(roleTable)', function (obj) {
                if (obj.event === 'edit') { // 修改
                    showEditModel(obj.data);
                } else if (obj.event === 'auth') {  // 权限管理
                    showPermModel(obj.data.Id);
                }
            });

            /* 表格头工具栏点击事件 */
            table.on('toolbar(roleTable)', function (obj) {
                if (obj.event === 'add') { // 添加
                    showEditModel();
                } else if (obj.event === 'del') { // 删除
                    var checkRows = table.checkStatus('roleTable');
                    if (checkRows.data.length === 0) {
                        layer.msg('请选择要删除的数据', { icon: 2 });
                        return;
                    }
                    var ids = checkRows.data.map(function (d) {
                        return d.Id;
                    });
                    doDel({ ids: ids });
                } else if (obj.event == 'export') {
                    // 导出
                    var cols = [[
                        { field: 'Remark', title: '字典描述', width: 300 },
                        { field: 'DictType', title: '字典类型' },
                        {
                            field: 'CreateTime', title: '创建时间', templet: function (d) {
                                return util.toDateString(d.CreateTime);
                            }
                        },
                    ]];
                    layuiTableExport(cols, setter.baseServer + '/SystemManage/Role/GetListJson', '角色导出')
                }
            });

            var ChangeId = 0;
            /* 显示表单弹窗 */
            function showEditModel(mData) {
                admin.open({
                    type: 1,
                    title: (mData ? '修改' : '添加') + '角色',
                    content: $('#roleEditDialog').html(),
                    success: function (layero, dIndex) {
                        // 回显表单数据
                        form.val('roleEditForm', mData);
                        ChangeId = mData ? mData.Id : 0;

                        // 表单提交事件
                        form.on('submit(roleEditSubmit)', function (data) {
                            var loadIndex = layer.load(2);
                            data.field.Id = ChangeId;
                            admin.req('/SystemManage/Role/SaveFormJson', data.field, function (res) {
                                layer.close(loadIndex);
                                if (1 === res.Tag) {
                                    layer.close(dIndex);
                                    layer.msg("操作成功", { icon: 1 });
                                    insTb.reload({ page: { curr: 1 } });

                                    layui.sessionData('UserFormFlushFlag', {
                                        key: 'UserFormFlushFlag'
                                        , value: '1'
                                    });

                                } else {
                                    layer.msg(res.Message, { icon: 2 });
                                }
                            }, 'post');
                            return false;
                        });
                    }
                });
            }

            /* 删除 */
            function doDel(obj) {
                layer.confirm('确定要删除选中数据吗？', {
                    skin: 'layui-layer-admin',
                    shade: .1
                }, function (i) {
                    layer.close(i);
                    var loadIndex = layer.load(2);
                    admin.req('/SystemManage/Role/DeleteFormJson', obj.ids.join(','), function (res) {
                        layer.close(loadIndex);
                        if (1 === res.Tag) {
                            layer.msg("操作成功", { icon: 1 });
                            insTb.reload({ page: { curr: 1 } });
                        } else {
                            layer.msg(res.Message, { icon: 2 });
                        }
                    }, 'post');
                });
            }

            /* 权限管理 */
            function showPermModel(roleId) {
                admin.open({
                    title: '角色权限分配',
                    btn: ['保存', '取消'],
                    content: '<ul id="roleAuthTree" class="ztree"></ul>',
                    success: function (layero, dIndex) {
                        var loadIndex = layer.load(2);
                        admin.req('/SystemManage/Role/QueryRoleAuthTree', { id: roleId }, function (res) {
                            layer.close(loadIndex);
                            if (1 === res.Tag) {
                                $.fn.zTree.init($('#roleAuthTree'), {
                                    check: { enable: true },
                                    data: { simpleData: { enable: true } }
                                }, res.Data);
                            } else {
                                layer.msg(res.Message, { icon: 2 });
                            }
                        }, 'get');
                        // 超出一定高度滚动
                        $(layero).children('.layui-layer-content').css({ 'max-height': '300px', 'overflow': 'auto' });
                    },
                    yes: function (dIndex) {
                        var insTree = $.fn.zTree.getZTreeObj('roleAuthTree');
                        var checkedRows = insTree.getCheckedNodes(true);
                        var ids = [];
                        for (var i = 0; i < checkedRows.length; i++) {
                            ids.push(checkedRows[i].id);
                        }
                        var loadIndex = layer.load(2);
                        admin.req('/SystemManage/Role/SaveRoleAuth/' + roleId, { ids: ids.join(',') }, function (res) {
                            layer.close(loadIndex);
                            if (1 === res.Tag) {
                                layer.msg("操作成功", { icon: 1 });
                                layer.close(dIndex);
                            } else {
                                layer.msg(res.Message, { icon: 2 });
                            }
                        }, 'post');
                    }
                });
            }
        });
    </script>


</body>
</html>